<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
        
        function __construct()
        {
            parent::__construct();
            $this->load->model('modelo');
            $this->load->library('cart');
        }
        public function index()
        {
            /*$this->load->view('login');*/
            $this->load->view('header2');
            $this->load->view('content');
            $this->load->view('footer2');
        }
         
        function Login()
        {
            $nickname = $this->input->post('nickname');
            $clave = md5($this->input->post('clave'));
            
            $numero = $this->modelo->consulta($nickname,$clave)->num_rows();
            $msj="false";
            if($numero >0):
                $permiso = $this->modelo->consultaPermiso($nickname,$clave)->result();
                foreach($permiso as $row):
                    $permiso = $row->Permiso;
                endforeach;
                $data   =   array(
                    'username'  => $nickname,
                    'logged_in' => TRUE,
                    'permiso' => $permiso
                );
                $this->session->set_userdata($data);
                $msj="true";
            endif;

            if($this->session->userdata('logged_in')==TRUE):
                $msj = 'true';
            else:
                $msj = 'false';
            endif;
            echo json_encode(array('msje'=>$msj));
        }
        function cargaRegistro()
        {
            if($this->session->userdata('permiso')=='0'):
                $this->load->view('header');
                $data['nombre'] = $this->session->userdata('username');
                //$this->load->view('principal',$data);
                $this->load->view('registro',$data);
                $this->load->view('footer');
            else:
                $this->load->view('header');
                $this->load->view('footer');
            endif;
        }
        function verificaLogin()
        {
            if($this->session->userdata('logged_in')==TRUE):
                $msj = 'true';
            else:
                $msj = 'false';
            endif;
            echo json_encode(array('msje'=>$msj));
        }
        function logout()
        {
            $this->session->sess_destroy();
            if($this->session->userdata('logged_in')==TRUE):
                $msj = 'true';
            else:
                $msj = 'false';
            endif;
            echo json_encode(array('msje'=>$msj));
            redirect(base_url());
        }

        function mostrarProductos()
        {       
                $local=$this->input->post('contador');
                $filtro=$this->input->post('filtro');
                $contador=0;
                $agregados=0;
                $todos="todos";
                $dato = $this->modelo->consultaProducto()->result();
                echo "<table border=0>";
                echo "<tr>";
                $cuenta =1;
                foreach($dato as $row):
                    if($contador>=$local){
                            $quees=$row->prod_clase;
                            if(strcmp($filtro,$quees)===0){
                                echo "<td width=\"190\" height=\"160\" valign=\"top\"><center><b> ",
                                                $row->prod_nombre,"</b><br/>",
                                                "<img src=\"",base_url(),"imagenes/",$row->prod_codigo,".jpg\" width=\"100\" height=\"100\" /></img><br/>",
                                                $row->prod_descripcion,"<br />",
                                                "<a href=\"#\" onclick=\"agregarCarro('", $row->prod_codigo,"');\"><img src=\"",base_url(),"imagenes/vercarrito.gif\" /></img></a>",
                                         "</center></td>";
                                $agregados+=1;
                                $cuenta=$cuenta+1;
                                if ($cuenta === 5){
                                    echo "</tr>";
                                    echo "<tr>";
                                    $cuenta=1;
                                } 
                            }
                            elseif (strcmp($filtro,$todos)===0) {
                                echo "<td width=\"190\" height=\"160\" valign=\"top\"><center><b> ",
                                                $row->prod_nombre,"</b><br/>",
                                                "<img src=\"",base_url(),"imagenes/",$row->prod_codigo,".jpg\" width=\"100\" height=\"100\" /></img><br/>",
                                                $row->prod_descripcion,"<br />",
                                                "<a href=\"#\" onclick=\"agregarCarro('", $row->prod_codigo,"');\"><img src=\"",base_url(),"imagenes/vercarrito.gif\" /></img></a>",
                                         "</center></td>";
                                $agregados+=1;
                                $cuenta=$cuenta+1;
                                if ($cuenta === 5){
                                    echo "</tr>";
                                    echo "<tr>";
                                    $cuenta=1;
                                }                            
                                
                            }
                    }
                    $contador+=1;
                    if($agregados===8){
                        break;
                    }                 
                endforeach;  
                echo"</tr>";
                echo "</table>";
                echo "<center>";  
                if($contador>8 && strcmp($filtro,$todos)===0){
                    echo "<a href=\"#\" onclick=\"productos('",$contador-$agregados-8,"','",$filtro,"');\">Anterior</a>";}
                elseif ($contador>8){
                    echo "<a href=\"#\" onclick=\"productos('",$agregados-8,"','",$filtro,"');\">Anterior</a>";}
               
                if($agregados>=8){
                echo "   <a href=\"#\" onclick=\"productos('",$contador,"','",$filtro,"');\">Siguiente</a></center>";}

        }
        function agregaCarro(){
               $id = $this->input->post('nombre');
               $dato = $this->modelo->devolverProductos($id)->result();
               foreach($dato as $row):          
               $data = array(
                    'id'      => $id,
                    'qty'     => 1,
                    'price'   =>  $row->prod_precio_compra,
                    'name'    => $row->prod_nombre,
                );
               endforeach;
                $this->cart->insert($data);
                 echo   "<table width=\"720\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\"> ";
                 echo    "<tr bgcolor=\"#333333\" class=\"tit\"> ";
                 echo       "<td width=\"300\">Producto</td> ";
                 echo       "<td width=\"207\">Precio</td>";
                 echo       "<td width=\"207\" align=\"center\">Cantidad de Unidades</td>";
                 echo       "<td width=\"100\" align=\"center\">Borrar</td>";                         
                 echo    "</tr> ";
                foreach ($this->cart->contents()  as $items):
                        echo"<tr  bgcolor=\"#ffffff\" class=\"prod\">";
                        echo "<td width=\"300\">",$items['name'],"</td> ";
                        echo "<td width=\"207\">",$items['price'],"</td>";
                        echo "<td width=\"207\" align=\"center\">",$items['qty'],"</td>";
                        echo "<td width=\"100\" align=\"center\"><img src=\"",base_url(),"imagenes/trash.gif\" /></img></td>";
                        echo "</tr>";
                 endforeach;
                 echo "</table>";
                 echo "<center> Total: ",$this->cart->total();
                 echo "<br>Cantidad de productos: ",$this->cart->total_items(),"</center>";
        }
        
        function borrarCarro(){
            $this->cart->destroy();
            echo "<center>carrito vacio</center>";
        }
        function guardaRegistro()
        {
            $nombre = $this->input->post('nombre');
            $rut    = $this->input->post('rut');
            $correo = $this->input->post('correo');
            $nickname = $this->input->post('nickname');
            $clave = md5($this->input->post('clave'));
            $permiso = $this->input->post('permiso');
            $this->modelo->guardaRegistro($nombre,$rut,$correo,$nickname,$clave,$permiso);
            $usuarios = $this->modelo->buscaUsuarios()->result();
            $i=0;
            foreach ($usuarios as $row):
                $data['nombre'.$i] = $row->Nombre;
                $data['correo'.$i] = $row->Correo;
                $data['rut'.$i] = $row->Rut;
                $i=$i+1;
            endforeach;
            $data['indice']=$i;
            $this->load->view('informe',$data);
            //echo $nombre.' '.$rut.' '.$correo.' '.$nickname.' '.$clave.' '.$permiso;
        }
        function registrar()
        {
            $usuarios = $this->modelo->buscaUsuario()->result();
            $i=0;
            foreach ($usuarios as $row):
                $datos['nickname'.$i] = $row->Nickname;
                $datos['clave'.$i] = $row->Clave;
                $i++;
            endforeach;
            $datos["indice"]=$i;
            $this->load->view('registrar',$datos);
        }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */